<template>
  <div>
    <a-card :bordered="false" :bodyStyle="tstyle">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="24">
              <a-form-item label="">
                <a-range-picker @change="onDateChange" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="">
                <a-radio-group  v-model="queryParam.receiveDateType" button-style="solid">
                  <a-radio-button value="1">
                    實收日期
                  </a-radio-button>
                  <a-radio-button value="2">
                    應收日期
                  </a-radio-button>
                </a-radio-group>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="收支">
                  <a-select style="width: 100%" v-model="queryParam.billInOutType" placeholder="请选择收支方式">
                    <a-select-option  :value="0">全部</a-select-option>
                    <a-select-option  :value="1">流入</a-select-option>
                    <a-select-option  :value="2">流出</a-select-option>
                  </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="来源">
                  <a-select style="width: 100%" v-model="queryParam.source" placeholder="请选择来源">
                    <a-select-option v-for="(item,index) in sourceData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
                  </a-select>
                </a-form-item>
            </a-col>
          </a-row>
          <a-row :gutter="48">
            <a-col :md="6" :sm="24">
              <a-form-item label="交易方式">
                  <a-select style="width: 100%" v-model="queryParam.transaction" placeholder="请选择交易方式" >
                    <a-select-option value="0">全部交易</a-select-option>
                    <a-select-option v-for="(item,index) in collectionTypeData" :key="index" :value="item.id">{{ item.name }}</a-select-option>
                  </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
                <a-form-item label="审核标志">
                  <a-select style="width: 100%" v-model="queryParam.auditFlag" placeholder="请选择审核标志"  >
                    <a-select-option  :value="2">全部審核</a-select-option>
                    <a-select-option  :value="0">未審核</a-select-option>
                    <a-select-option  :value="1">已審核</a-select-option>
                  </a-select>
                </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="房產">
                  <a-select style="width: 100%"  v-model="queryParam.stateId" @change="handleStateChange" placeholder="请选择房產">
                    <a-select-option value="0" :key="'info1-'+0" >全部房產</a-select-option>
                    <a-select-option v-for="(item,index) in stateData" :key="'info1-'+index"  :value="item.id">{{ item.name }}</a-select-option>
                  </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="房閒">
                  <a-select style="width: 100%" v-model="queryParam.roomId" @change="handleRoomChange" placeholder="请选择房閒">
                    <a-select-option value="0" :key="'info2-'+0" >全部房閒</a-select-option>
                    <a-select-option v-for="(item,index) in roomData" :key="'info2-'+index"  :value="item.id">{{ item.code }}</a-select-option>
                  </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24" >
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)" >查询</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false">
      <s-table
        ref="table"
        size="default"
        :columns="columns"
        :data="loadData"
        :alert="true"
        :scroll="{ x: 1500}"
        :rowKey="(record) => record.id">
        <template class="table-operator" slot="operator">
          <a-row>
            <a-col :span="16">
                <!-- <span>流入合計：5454</span><a-divider type="vertical" /><span>流出合計：5454</span><a-divider type="vertical" /><span>結餘：5454</span> -->
            </a-col>
            <a-col :span="8">
               <a-button @click="exportExcelByType" type="primary" class="bun" size="small">
                導出
              </a-button>
            </a-col>
          </a-row>
        </template>
        <span slot="sourcescopedSlots" slot-scope="text">
          {{ 'bill_water_source' | dictType(text) }}
        </span>
        <span slot="amount" slot-scope="text">
            <span :class="text < 0 ? 'abvv': 'ab'">{{ text }}</span>
        </span>
         <span slot="transaction" slot-scope="text">
          {{ statusFkfsFilter(text) }}
        </span>
        <span slot="auditFlagscopedSlots" slot-scope="text">
          {{ 'bill_audit_flag' | dictType(text) }}
        </span>
        <span slot="action" slot-scope="text, record">
          <a @click="$refs.editForm.edit(record.id)">查看</a>
          <a-divider type="vertical"/>
          <a v-if="record.auditFlag === 0" @click="shenhe(record.id, 1)">審核</a>
          <a v-if="record.auditFlag === 1" @click="shenhe(record.id, 2)">反審</a>
          <a-divider type="vertical"/>
          <a v-if="record.source === 1" @click="$refs.houseBillForm.edit(record.billId)">上游單據</a>
          <a v-if="record.source === 3 || record.source === 4" @click="$refs.lookDetail.edit(record.billId)">上游單據</a>
          <a v-if="record.source === 7" @click="$refs.readOnlyForm.edit(record.billId)">上游單據</a>
        </span>
      </s-table>
      <edit-form ref="editForm" />
      <house-bill-form ref="houseBillForm" />
      <read-only-form ref="readOnlyForm"/>
      <look-detail ref="lookDetail"/>
    </a-card>
  </div>
</template>
<script>
  import { STable } from '@/components'
  import moment from 'moment'
  import { billWaterPage, billWaterDelete, shenhe, exportExcel } from '@/api/modular/main/billwater/billWaterManage'
  import { busMeansOfTransactionList } from '@/api/modular/main/busmeansoftransaction/busMeansOfTransactionManage'
  import { getAllStateList, getStateRoomListByStateId } from '@/api/modular/main/busrealestate/busRealEstateManage'
  import houseBillForm from './houseBillForm.vue'
  import editForm from './editForm.vue'
  import readOnlyForm from '../ownerleasebill/readOnlyForm.vue'
  import lookDetail from '../billotherrevenues/lookDetail.vue'
  export default {
    components: {
      STable,
      editForm,
      houseBillForm,
      readOnlyForm,
      lookDetail
    },
    data () {
      return {
        // 高级搜索 展开/关闭
        advanced: false,
        // 查询参数
        queryParam: {
          dateStrStart: '',
          dateStrEnd: '',
          receiveDateType: '1',
          billInOutType: 0,
          source: '0',
          transaction: '0',
          auditFlag: 2,
          stateId: '0',
          roomId: '0'
        },
        // 表头
        columns: [
          {
            title: '房间名',
            align: 'center',
            dataIndex: 'roomName',
            key: 'roomName',
            width: 150
          },
          {
            title: '姓名',
            align: 'center',
            dataIndex: 'name',
            key: 'name',
            width: 100
          },
          {
            title: '来源',
            align: 'center',
            dataIndex: 'source',
            scopedSlots: { customRender: 'sourcescopedSlots' },
            key: '1',
            width: 100
          },
          {
            title: '金额',
            align: 'center',
            dataIndex: 'amount',
            scopedSlots: { customRender: 'amount' },
            key: '2',
            width: 100
          },
          {
            title: '應收日期',
            align: 'center',
            dataIndex: 'shouldReceiveDate',
            key: '3',
            width: 150
          },
          {
            title: '實收日期',
            align: 'center',
            dataIndex: 'receiveDate',
            key: '4',
            width: 150
          },
          {
            title: '交易方式',
            align: 'center',
            dataIndex: 'transaction',
            scopedSlots: { customRender: 'transaction' },
            key: '5',
            width: 100
          },
          {
            title: '经办人',
            align: 'center',
            dataIndex: 'manager',
            key: '6',
            width: 150
          },
          {
            title: '审核标志',
            align: 'center',
            dataIndex: 'auditFlag',
            scopedSlots: { customRender: 'auditFlagscopedSlots' },
            key: '7',
            width: 100
          },
          {
            title: '审核人',
            align: 'center',
            dataIndex: 'auditName',
            key: '8',
            width: 100
          },
          {
            title: '审核时间',
            align: 'center',
            dataIndex: 'auditTime',
            key: '9',
            width: 150
          },
          {
            title: '操作',
             key: 'action',
            dataIndex: 'action',
            scopedSlots: { customRender: 'action' },
            width: 180
          }
        ],
        tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return billWaterPage(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        },
        sourceData: [],
        transactionData: [],
        auditFlagData: [],
        selectedRowKeys: [],
        selectedRows: [],
        collectionTypeData: [],
        stateData: [],
        roomData: []
      }
    },
    created () {
      this.initData()
      const sourceOption = this.$options
      this.sourceData = sourceOption.filters['dictData']('bill_water_source')
      const transactionOption = this.$options
      this.transactionData = transactionOption.filters['dictData']('common_status')
      const auditFlagOption = this.$options
      this.auditFlagData = auditFlagOption.filters['dictData']('common_status')
    },
    methods: {
      moment,
      onDateChange(date, dateString) {
        const s1 = dateString[0]
        const s2 = dateString[1]
        this.queryParam.dateStrStart = s1
        this.queryParam.dateStrEnd = s2
      },
      getAllStateList() {
        getAllStateList().then((res) => {
          if (res.code === 200) {
            this.stateData = res.data
          } else {
            this.$message.error(res.msg)
          }
        })
      },
      initData() {
         busMeansOfTransactionList().then((res) => {
          if (res.code === 200) {
            this.collectionTypeData = res.data
          } else {
            this.$message.error(res.msg)
          }
        })
        this.getAllStateList()
      },
      statusFkfsFilter (id) {
        // eslint-disable-next-line eqeqeq
        const values = this.collectionTypeData.filter(item => item.id == id)
        if (values.length > 0) {
          return values[0].name
        }
      },
      handleStateChange(value) {
        if (value !== '0') {
          this.getAllStateRoomList(value)
        }
        this.queryParam.roomId = '0'
      },
      handleRoomChange(value) {
         this.queryParam.roomId = value
      },
      queryData() {
        this.$refs.table.refresh(true)
      },
      getAllStateRoomList(id) {
        getStateRoomListByStateId(id).then((res) => {
          if (res.code === 200) {
            this.roomData = res.data
          } else {
            this.$message.error(res.msg)
          }
        })
      },
     shenhe(id, type) {
       shenhe(id, type).then((res) => {
          if (res.code === 200) {
            this.$message.success('审核成功')
            this.$refs.table.refresh()
          } else {
            this.$message.error(res.msg) // + res.message
          }
        })
     },
     billWaterDelete (record) {
        billWaterDelete(record).then((res) => {
          if (res.code === 200) {
            this.$message.success('删除成功')
            this.$refs.table.refresh()
          } else {
            this.$message.error('删除失败') // + res.message
          }
        })
      },
      exportExcelByType() {
         exportExcel(this.queryParam).then((res) => {
             this.downloadfile(res)
        })
      },
      downloadfile (res) {
        var blob = new Blob([res.data], { type: 'application/octet-stream;charset=UTF-8' })
        var contentDisposition = res.headers['content-disposition']
        var patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*')
        var result = patt.exec(contentDisposition)
        var filename = result[1]
        var downloadElement = document.createElement('a')
        var href = window.URL.createObjectURL(blob) // 创建下载的链接
        var reg = /^["](.*)["]$/g
        downloadElement.style.display = 'none'
        downloadElement.href = href
        downloadElement.download = decodeURI(filename.replace(reg, '$1')) // 下载后文件名
        document.body.appendChild(downloadElement)
        downloadElement.click() // 点击下载
        document.body.removeChild(downloadElement) // 下载完成移除元素
        window.URL.revokeObjectURL(href)
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      onChangeoperatingDate(date, dateString) {
        this.operatingDateDateString = dateString
      },
      handleOk () {
        this.$refs.table.refresh()
      },
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      }
    }
  }
</script>
<style lang="less" scoped>
  .table-operator {
    margin-bottom: 18px;
  }
  button {
    margin-right: 8px;
  }
  .bun{
    float: right;
  }
  .ab{
    color: red;
  }
  .abvv{
    color: green;
  }
</style>
